package testDemo.IOStream.ObjectStream;

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //读取对象到文件，序列化
//        Student s1 = new Student("张三",19);
//        Student s2 = new Student("李四",20);
//        Student s3 = new Student("王五",21);
//
//        ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("b.txt"));
//        oos.writeObject(s1);
//        oos.writeObject(s2);
//        oos.writeObject(s3);
//
//        oos.close();
    //读取文件 到对象，反序列化
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("b.txt"));
        System.out.println((Student) ois.readObject());
        System.out.println((Student) ois.readObject());
        System.out.println((Student) ois.readObject());
        ois.close();
    }
}
class Student implements Serializable{
    private static final long SerialVersionUID=4L;
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student [" +
                "name=" + name +
                ", age=" + age +
                "]";
    }
}
